/**
 * Copyright (C) T-Systems International, 2013-2014
 *
 * The copyright of the computer program herein is the property of
 * T-Systems International. The program may be used and/or copied
 * only with the written permission of T-Systems International or in
 * accordance with the terms and conditions stipulated in the
 * agreement/contract under which the program has been supplied.
 *
 * $Author: kuzin43 $
 * $Date: 2014-10-16 13:27:35 +0000 (Thu, 16 Oct 2014) $
 * $Rev: 84 $
 */
package com.tsystems.bsipp.dm.persistence.model;

import javax.persistence.*;
import java.util.Date;
import java.util.UUID;

/**
 * Base class for entities with UUID id type.
 */
@MappedSuperclass
public abstract class AbstractEntity {

    /**
     * UUID description.
     */
    @Id
    @Column(columnDefinition = "char", length = ModelConstants.ID_LENGTH)
    private String id;

    /**
     * method generates a new uuid instead of JPA annotation.
     */
    @PrePersist
    public void generateUUID() {
        this.id = UUID.randomUUID().toString();
    }

    /**
     * Returns the id of the entity. The entity is considered unpersisted if it is {@code null}.
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the if of the entity.
     * @param id the id of entity
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Checks for equality of this object and another one. It checks only by equalitu of ids.
     *
     * @param o the object to compare with
     * @return true if {@code o} is not null, it is also an entity and the ids are equal.
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AbstractEntity that = (AbstractEntity) o;

        return getId() != null ? getId().equals(that.getId()) : that.getId() == null;

    }

    /**
     * Returns the hash code of the object. It calculates the hash code by id of entity.
     *
     * @return the hash code of entity.
     */
    @Override
    public int hashCode() {
        return getId() != null ? getId().hashCode() : 0;
    }
}
